<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017/11/20
 * Time: 16:21
 */

namespace app\admin\model;

use think\Model;

class BankAccount extends Model
{
    /**
     * 获取所有记录
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getAll(){
       
        $order = '';
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where['id_card|user.tel|user.full_name'] = array('like' ,"%{$keywords}%" );
        }else{
            $where = [];
        }
        $data = $this
            ->alias("bank")
            ->join(' user user','bank.user_id=user.id','LEFT')
            ->field('bank.id,bank.user_id,user.full_name,bank_card,bank_code')
            ->where($where)
            ->order($order)
            ->select();

        if($data){

            $bank_codes = config("bank");//获取extra/bank.php 文件中所有的银行编号

            foreach($data as $key => $value){
                foreach($bank_codes as $k => $v){
                    if($data[$key]['bank_code'] == $k){
                        $data[$key]['bank_code'] = $bank_codes[$k];
                    }
                }
                $data[$key] = $value->toArray();
            }
        }
        return $data;
    }

    /**
     * 获取记录数
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getCount($where=[],$order=""){
        $count = $this
            ->alias("bank")
            ->join(' user user','bank.user_id=user.id','LEFT')
            ->field('bank.id,,bank.user_id,user.full_name,bank_card,bank_code')
            ->where($where)
            ->order($order)
            ->count();
        return $count;
    }

/**
 * 获取银行账户的的详情
 * @author 巫玉梅 <1120504614@qq.com>
 * @return
 */
    public function getOne($where=""){
        $info = $this
            ->alias("bank")
            ->join('user user','bank.user_id=user.id','LEFT')
            ->field('bank.*,user.full_name')
            ->where($where)
            ->find();
        if($info){
            $info = $info -> toArray();
        }
        return $info;
    }
    /**
     * 通过ID获取银行账户的的详情
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getBankAccountById($id){
        $info = $this
            ->alias("bank")
            ->join('user user','bank.user_id=user.id','LEFT')
            ->field('bank.*,user.full_name')
            ->where('bank.id='.$id)
            ->find();
        if($info){
            $info = $info -> toArray();
        }
        return $info;
    }
}